package com.zero_copy;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Scanner;

/**
 * @author: csh
 * @Date: 2020/10/22 14:19
 * @Description:java零拷贝
 */
public class ZeroCopy {
    public static void main(String[] args) throws Exception {
        File file = new File("file.txt");
        long len = file.length();
        byte[] ds = new byte[(int)len];
        //mappedByteBuffer为零拷贝
        MappedByteBuffer mappedByteBuffer = new FileInputStream(file).
                getChannel().map(FileChannel.MapMode.READ_ONLY,0,len);
        for (int offset = 0; offset < len; offset++) {
            byte b = mappedByteBuffer.get();
            ds[offset] = b;
        }
        Scanner scanner = new Scanner(new ByteArrayInputStream(ds)).useDelimiter("");
        while (scanner.hasNext()){
            System.out.println(scanner.next()+" ");
        }
    }
}
